package com.lanju.dao;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.lanju.entity.AllFree;
import com.lanju.entity.User;

@Repository
public class AllFreeDao {

	private static final Log log = LogFactory.getLog(AllFreeDao.class);
	@Autowired
	SessionFactory sessionFactory;
	
	@PersistenceContext(unitName = "financialmarketingUnit")
	private EntityManager entityManager;
	
	public void saveFree(AllFree af){
		try {
			entityManager.persist(af);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void updateFree(AllFree af){
		try {
			this.sessionFactory.getCurrentSession().update(af);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void deleteFree(String id){
		try {
			StringBuffer sql = new StringBuffer();
			sql.append("delete from AllFree where free_id='"+id+"'");
			entityManager.createQuery(sql.toString()).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public AllFree getAllFreeById(String id){
		try {
			Query query = entityManager.createQuery("from AllFree where free_id='"+id+"'");
			return (AllFree) query.getSingleResult();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<AllFree> getAllFreeList(Map<String,Object> map){
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" from AllFree  where 1=1 ");
			if(!StringUtils.isEmpty(map.get("month"))){
				sql.append(" and month like '%"+map.get("month").toString()+"%'");
			}
			sql.append(" order by create_time desc");
			Query query = entityManager.createQuery(sql.toString());
			return query.getResultList();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public Long getAllFreeListCount(Map<String,Object> map){
		try {
			StringBuffer sql = new StringBuffer();
			sql.append(" from AllFree  where 1=1 ");
			if(!StringUtils.isEmpty(map.get("userName"))){
				sql.append(" and userName like '%"+map.get("userName").toString()+"%'");
			}
			sql.append(" order by create_time desc");
			Query query = entityManager.createQuery(sql.toString());
			return (long) query.getResultList().size();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public List<User> getAllUserList(Map<String,Object> param){
		List<User> h = null;
		try {
			StringBuffer sql=new StringBuffer();
			sql.append("from User where flag=0 ");
			if(!StringUtils.isEmpty(param.get("text"))){
			sql.append(" and nickName like '%"+param.get("text").toString()+"%'");	
			}
			Query query=entityManager.createQuery(sql.toString());
			if(param.get("limit") != null){
				h = query.getResultList();
			}else{
				h = query.setFirstResult(0).setMaxResults(10).getResultList();
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return h;
	}
}
